﻿using Common;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.HospitalizedChildren
{
    public partial class PrintHospitalizedChildren : Form
    {
        GridppReport Report = new GridppReport();
        NursingForm formService = new NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string Id;
        ElectronicSignature es;
        public PrintHospitalizedChildren(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            Id = id;
            es = new ElectronicSignature(this.LoginUser);
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\儿童护理评估单.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecord(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.Childs);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(this.Id))
                    list = list.Where(p => p.Id.Equals(Convert.ToInt32(this.Id))).ToList();
                CreateDataTableHead(dt, list);
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    Child fd = XmlUtil.XMLToObject<Child>(record.NursingFormData, Encoding.UTF8);
                    string image1 = "";
                    string qianming1 = fd.DieDaoYaChuangADLFengXianPingGuC.RuYuanHuShiQianMing;
                    if (!string.IsNullOrEmpty(qianming1))
                    {
                        image1 = es.GetElectricSignBase64(record.AreaId, qianming1);
                    }
                    string image2 = "";
                    string qianming2 = fd.ChuYuanPingGuC.ChuYuanHuShiQianMing;
                    if (!string.IsNullOrEmpty(qianming2))
                    {
                        image2 = es.GetElectricSignBase64(record.AreaId, qianming2);
                    }

                    if (fd.SheHuiPingGuC.PaiNiao.Split('#').Length == 8)
                        fd.SheHuiPingGuC.PaiNiao = fd.SheHuiPingGuC.PaiNiao += "#0";

                    if (fd.SheHuiPingGuC.YinShi_LeiXing.Split('#').Length == 9)
                        fd.SheHuiPingGuC.YinShi_LeiXing = fd.SheHuiPingGuC.YinShi_LeiXing += "#0";


                    dt.Rows.Add(
                        PatientInfo.PATIENT_ID,
                        PatientInfo.PATIENT_NAME,
                        PatientInfo.SEX,
                        PatientInfo.AGE,
                        PatientInfo.BED_NO,
                        fd.JiBenXinXiC.RuYuanShiJian,
                        PatientInfo.OUT_DATE,
                        fd.JiBenXinXiC.LianXiDianHua,
                        PatientInfo.DEPARTMENT_NAME,
                        PatientInfo.NURSE_CELL_NAME,
                        fd.JiBenXinXiC.RuYuanZhenDuan,
                        fd.JiBenXinXiC.MinZu,
                        PatientInfo.MARRIAGE,
                        PatientInfo.JOB,
                        PatientInfo.EDUCATION,
                        fd.JiBenXinXiC.LianXiRen,
                        fd.JiBenXinXiC.YuHuanZheGuanXi,
                        fd.TiZhengXinXiC.ShenZhi,
                        fd.TiZhengXinXiC.ShenZhiText,
                        fd.TiZhengXinXiC.JingShen,
                        fd.TiZhengXinXiC.KouQiang,
                        fd.TiZhengXinXiC.KouQiangText,
                        fd.TiZhengXinXiC.KouChun,
                        fd.TiZhengXinXiC.KouChunText,
                        fd.TiZhengXinXiC.PiFu,
                        fd.TiZhengXinXiC.PiFu,
                        fd.TiZhengXinXiC.PiFuText,
                        fd.SheHuiPingGuC.YinShi_ShiYu,
                        fd.SheHuiPingGuC.YinShi_FangShi,
                        fd.SheHuiPingGuC.YinShi_LeiXing,
                        fd.SheHuiPingGuC.HuoDong,
                        fd.SheHuiPingGuC.ShuiMian,
                        fd.SheHuiPingGuC.ZiLi,
                        fd.SheHuiPingGuC.PaiBian,
                        fd.SheHuiPingGuC.PaiNiao,
                        fd.SheHuiPingGuC.DaoGuan,
                        fd.SheHuiPingGuC.DaoGuan_Text,
                        fd.SheHuiPingGuC.ShengHuoHuanJing,
                        fd.SheHuiPingGuC.ShengHuoHuanJing_Text,
                        fd.SheHuiPingGuC.FeiYongLaiYuan,
                        fd.SheHuiPingGuC.FeiYongLaiYuan_Text,
                        fd.SheHuiPingGuC.HuanErHuoJiaZhangDuiJiangYaoJieShouDeYiLiaoFuWuShiFouYouJingJiDeYouLv,
                        fd.DieDaoYaChuangADLFengXianPingGuC.DieDaoPingGu,
                        fd.DieDaoYaChuangADLFengXianPingGuC.DieDaoPingGuFenShu,
                        fd.DieDaoYaChuangADLFengXianPingGuC.YaChuangPingGu,
                        fd.DieDaoYaChuangADLFengXianPingGuC.YaChuangPingGuFenShu,
                        fd.DieDaoYaChuangADLFengXianPingGuC.YuFangJieZhong,
                        fd.DieDaoYaChuangADLFengXianPingGuC.YuFangJieZhong_Text,
                        fd.DieDaoYaChuangADLFengXianPingGuC.GuoMingShi,//过敏史-未发现
                        fd.DieDaoYaChuangADLFengXianPingGuC.GuoMingShi,//药物
                        fd.DieDaoYaChuangADLFengXianPingGuC.GuoMinShi_YaoWu_Text,
                        fd.DieDaoYaChuangADLFengXianPingGuC.GuoMingShi,//食物
                        fd.DieDaoYaChuangADLFengXianPingGuC.GuoMinShi_ShiWu_Text,
                        fd.DieDaoYaChuangADLFengXianPingGuC.GuoMingShi,//其他
                        fd.DieDaoYaChuangADLFengXianPingGuC.GuoMinShi_QiTa_Text,
                        fd.DieDaoYaChuangADLFengXianPingGuC.JiWangShi,
                        fd.DieDaoYaChuangADLFengXianPingGuC.JiWangShi_Text,
                        fd.DieDaoYaChuangADLFengXianPingGuC.RuYuanHuShiQianMing,
                        fd.DieDaoYaChuangADLFengXianPingGuC.RuYuanHuShiQianMing_ShiJian,
                        (string.IsNullOrEmpty(fd.ChuYuanPingGuC.ChuYuanRiQi) || fd.ChuYuanPingGuC.ChuYuanRiQi.StartsWith("0") ? "" : Convert.ToDateTime(fd.ChuYuanPingGuC.ChuYuanRiQi).ToString("yyyy")),
                        (string.IsNullOrEmpty(fd.ChuYuanPingGuC.ChuYuanRiQi) || fd.ChuYuanPingGuC.ChuYuanRiQi.StartsWith("0") ? "" : Convert.ToDateTime(fd.ChuYuanPingGuC.ChuYuanRiQi).ToString("MM")),
                        (string.IsNullOrEmpty(fd.ChuYuanPingGuC.ChuYuanRiQi) || fd.ChuYuanPingGuC.ChuYuanRiQi.StartsWith("0") ? "" : Convert.ToDateTime(fd.ChuYuanPingGuC.ChuYuanRiQi).ToString("dd")),
                        fd.ChuYuanPingGuC.ChuYuanZhenDuan,
                        fd.ChuYuanPingGuC.ZiLiNengLi,
                        fd.ChuYuanPingGuC.ChuYuanHouQuXiang,
                        fd.ChuYuanPingGuC.JuJiaTiaoJian,
                        fd.ChuYuanPingGuC.ChuYuanHuShiQianMing,
                        Convert.ToDateTime(fd.JiBenXinXiC.RuYuanShiJian).ToString("yyyy"),
                        Convert.ToDateTime(fd.JiBenXinXiC.RuYuanShiJian).ToString("MM"),
                        Convert.ToDateTime(fd.JiBenXinXiC.RuYuanShiJian).ToString("dd"),
                        Convert.ToDateTime(fd.JiBenXinXiC.RuYuanShiJian).ToString("HH"),
                        Convert.ToDateTime(fd.JiBenXinXiC.RuYuanShiJian).ToString("mm"),
                        fd.JiBenXinXiC.RuYuanFangShi,
                        fd.JiBenXinXiC.JiGuan,
                        fd.JiBenXinXiC.T,
                        fd.JiBenXinXiC.P,
                        fd.JiBenXinXiC.R,
                        fd.JiBenXinXiC.BP1,
                        fd.JiBenXinXiC.BP2,
                        fd.JiBenXinXiC.TiZhong,
                        fd.JiBenXinXiC.ShenGao,
                        fd.JiBenXinXiC.SPO,
                        fd.SheHuiPingGuC.PaiNiao_Text
                        );
                    if (image1 != "" && dt.Columns.Contains("ElectricSignBase641"))
                    {
                        dt.Rows[0]["ElectricSignBase641"] = image1;
                    }
                    if (image2 != "" && dt.Columns.Contains("ElectricSignBase642"))
                    {
                        dt.Rows[0]["ElectricSignBase642"] = image2;
                    }
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt, null);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }
        /// <summary>
        /// 加载列头
        /// </summary>
        /// <param name="dt"></param>
        private void CreateDataTableHead(DataTable dt, List<NursingFormRecord> list)
        {
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("IN_DATE", typeof(string));
            dt.Columns.Add("OUT_DATE", typeof(string));
            dt.Columns.Add("PHONE", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("DIAGNOSE", typeof(string));
            dt.Columns.Add("NATIONALS", typeof(string));
            dt.Columns.Add("MARRIAGE", typeof(string));
            dt.Columns.Add("JOB", typeof(string));
            dt.Columns.Add("EDUCATION", typeof(string));
            dt.Columns.Add("LINKMAN", typeof(string));
            dt.Columns.Add("PATIENT_RELATION", typeof(string));
            dt.Columns.Add("ShenZhi", typeof(string));
            dt.Columns.Add("ShenZhi_Text", typeof(string));
            dt.Columns.Add("JingShen", typeof(string));
            dt.Columns.Add("KouQiang", typeof(string));
            dt.Columns.Add("KouQiang_Text", typeof(string));
            dt.Columns.Add("KouChun", typeof(string));
            dt.Columns.Add("KouChun_Text", typeof(string));
            dt.Columns.Add("PiFu", typeof(string));
            dt.Columns.Add("PiFu_QiTa", typeof(string));
            dt.Columns.Add("PiFu_Text", typeof(string));
            dt.Columns.Add("YinShi_ShiYu", typeof(string));
            dt.Columns.Add("YinShi_FangShi", typeof(string));
            dt.Columns.Add("YinShi_LeiXing", typeof(string));
            dt.Columns.Add("HuoDong", typeof(string));
            dt.Columns.Add("ShuiMian", typeof(string));
            dt.Columns.Add("ZiLi", typeof(string));
            dt.Columns.Add("PaiBian", typeof(string));
            dt.Columns.Add("PaiNiao", typeof(string));
            dt.Columns.Add("DaoGuan", typeof(string));
            dt.Columns.Add("DaoGuan_Text", typeof(string));
            dt.Columns.Add("SheHuiPingGu_ShengHuoHuanJing", typeof(string));
            dt.Columns.Add("SheHuiPingGu_ShengHuoHuanJing_Text", typeof(string));
            dt.Columns.Add("SheHuiPingGu_FeiYongLaiYuan", typeof(string));
            dt.Columns.Add("SheHuiPingGu_FeiYongLaiYuan_Text", typeof(string));
            dt.Columns.Add("SheHuiPingGu_HuanZheJiaZhe_YouLv", typeof(string));
            dt.Columns.Add("DieDaoFengXian", typeof(string));
            dt.Columns.Add("DieDaoFengXian_FenShu", typeof(string));
            dt.Columns.Add("YaChuanFengXian", typeof(string));
            dt.Columns.Add("YaChuanFengXian_FenShu", typeof(string));
            dt.Columns.Add("QiTaPingGu_YuFangJieZhong", typeof(string));
            dt.Columns.Add("QiTaPingGu_YuFangJieZhong_Text", typeof(string));
            dt.Columns.Add("QiTaPingGu_GuoMinShi_WeiFaXian", typeof(string));
            dt.Columns.Add("QiTaPingGu_YaoWu", typeof(string));
            dt.Columns.Add("QiTaPingGu_YaoWu_Text", typeof(string));
            dt.Columns.Add("QiTaPingGu_ShiWu", typeof(string));
            dt.Columns.Add("QiTaPingGu_ShiWu_Text", typeof(string));
            dt.Columns.Add("QiTaPingGu_QiTa", typeof(string));
            dt.Columns.Add("QiTaPingGu_QiTa_Text", typeof(string));
            dt.Columns.Add("QiTaPingGu_JiWangShi", typeof(string));
            dt.Columns.Add("QiTaPingGu_JiWangShi_Text", typeof(string));
            dt.Columns.Add("RuYuan_HuShiQianMing", typeof(string));
            dt.Columns.Add("RuYuan_ShiJian", typeof(string));
            dt.Columns.Add("ChuYuanRiQi_Nian", typeof(string));
            dt.Columns.Add("ChuYuanRiQi_Yue", typeof(string));
            dt.Columns.Add("ChuYuanRiQi_Ri", typeof(string));
            dt.Columns.Add("ChuYuanZhenDuan", typeof(string));
            dt.Columns.Add("ChuYuanPingGu_ZiLiNengLi", typeof(string));
            dt.Columns.Add("ChuYuanPingGu_ChuYuanHouQuXiang", typeof(string));
            dt.Columns.Add("ChuYuanPingGu_JuJiaTiaoJian", typeof(string));
            dt.Columns.Add("ChuYuanPingGu_HuShiQianMing", typeof(string));
            dt.Columns.Add("Ru_Nian", typeof(string));
            dt.Columns.Add("Ru_Yue", typeof(string));
            dt.Columns.Add("Ru_Ri", typeof(string));
            dt.Columns.Add("Ru_Shi", typeof(string));
            dt.Columns.Add("Ru_Fen", typeof(string));
            dt.Columns.Add("RuYuanFangShi", typeof(string));
            dt.Columns.Add("JiGuan", typeof(string));
            dt.Columns.Add("TiZheng_T", typeof(string));
            dt.Columns.Add("TiZheng_P", typeof(string));
            dt.Columns.Add("TiZheng_R", typeof(string));
            dt.Columns.Add("TiZheng_BP1", typeof(string));
            dt.Columns.Add("TiZheng_BP2", typeof(string));
            dt.Columns.Add("TiZheng_TiZhong", typeof(string));
            dt.Columns.Add("TiZheng_ShenGao", typeof(string));
            dt.Columns.Add("SPO", typeof(string));
            dt.Columns.Add("PaiNiao_Text", typeof(string));
            if (list == null) return;
            NursingFormRecord record = list[0];
            Child fd = XmlUtil.XMLToObject<Child>(record.NursingFormData, Encoding.UTF8);
            //判断是否为空，不为空则添加列
            string qianming1 = fd.DieDaoYaChuangADLFengXianPingGuC.RuYuanHuShiQianMing;
            if (!string.IsNullOrEmpty(qianming1))
            {
                dt.Columns.Add("ElectricSignBase641", typeof(string));
            }
            string qianming2 = fd.ChuYuanPingGuC.ChuYuanHuShiQianMing;
            if (!string.IsNullOrEmpty(qianming2))
            {
                dt.Columns.Add("ElectricSignBase642", typeof(string));
            }
        }

        private void PrintHospitalizedChildren_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
